﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomatedTestDataManagement.Models;
using System.Web.Routing;

namespace AutomatedTestDataManagement.Controllers
{
    public class SuiteScenariosController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /SuiteScenarios/

        public ActionResult Index()
        {
            var testsuitescenarios = db.TestSuiteScenarios.Include(t => t.TestScenario).Include(t => t.TestSuite);
            return View(testsuitescenarios.ToList());
        }

        //
        // GET: /SuiteScenarios/Details/5

        public ActionResult Details(int id = 0)
        {
            TestSuiteScenario testsuitescenario = db.TestSuiteScenarios.Find(id);
            if (testsuitescenario == null)
            {
                return HttpNotFound();
            }
            return View(testsuitescenario);
        }

        //
        // GET: /SuiteScenarios/Create

        public ActionResult Create(int id = 0)
        {
            if (id == 0)
            {
                ViewBag.SuiteID = new SelectList(db.TestSuites, "SuiteID", "SuiteName");
                ViewBag.ScenarioNumber = 0;

            }
            else
            {
                ViewBag.SuiteID = new SelectList(db.TestSuites, "SuiteID", "SuiteName", id);
                int maxNum = 0;
                if (db.TestSuiteScenarios.Where(t => t.SuiteID == id).Count() != 0)
                {
                    maxNum = db.TestSuiteScenarios.Where(t => t.SuiteID == id).Max(t => t.ScenarioNumber);
                }
                ViewBag.SelectedSuiteID = id;
                ViewBag.ScenarioNumber = maxNum + 1;
            }
            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName");
            return View();
        }

        //
        // POST: /SuiteScenarios/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TestSuiteScenario testsuitescenario)
        {
            if (ModelState.IsValid)
            {
                db.TestSuiteScenarios.Add(testsuitescenario);
                db.SaveChanges();
                return RedirectToAction("Edit", "Suite", new { id = testsuitescenario.SuiteID });
            }

            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", testsuitescenario.ScenarioID);
            ViewBag.SuiteID = new SelectList(db.TestSuites, "SuiteID", "SuiteName", testsuitescenario.SuiteID);
            return View(testsuitescenario);
        }

        //
        // GET: /SuiteScenarios/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TestSuiteScenario testsuitescenario = db.TestSuiteScenarios.Find(id);
            if (testsuitescenario == null)
            {
                return HttpNotFound();
            }
            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", testsuitescenario.ScenarioID);
            ViewBag.SuiteID = new SelectList(db.TestSuites, "SuiteID", "SuiteName", testsuitescenario.SuiteID);
            return View(testsuitescenario);
        }

        //
        // POST: /SuiteScenarios/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TestSuiteScenario testsuitescenario)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testsuitescenario).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ScenarioID = new SelectList(db.TestScenarios, "ScenarioID", "ScenarioName", testsuitescenario.ScenarioID);
            ViewBag.SuiteID = new SelectList(db.TestSuites, "SuiteID", "SuiteName", testsuitescenario.SuiteID);
            return View(testsuitescenario);
        }

        //
        // GET: /SuiteScenarios/Delete/5

        public ActionResult Delete(int id = 0, int suiteID = 0, int scenarioID = 0, string returnToAction = "Edit")
        {
            TestSuiteScenario testsuitescenario = db.TestSuiteScenarios.Find(id);
            if (testsuitescenario == null)
            {
                return HttpNotFound();
            }
            if (suiteID != 0) { ViewBag.SuiteID = suiteID; }
            if (scenarioID != 0) { ViewBag.ScenarioID = scenarioID; }
            ViewBag.ReturnToAction = returnToAction;
            return View(testsuitescenario);
        }

        //
        // POST: /SuiteScenarios/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var suiteID = Request.Form["SuiteID"];
            var scenarioID = Request.Form["ScenarioID"];
            var returnToAction = Request.Form["ReturnToAction"];
            if (returnToAction == null)
            {
                returnToAction = "Edit";
            }

            TestSuiteScenario testsuitescenario = db.TestSuiteScenarios.Find(id);
            db.TestSuiteScenarios.Remove(testsuitescenario);
            db.SaveChanges();
            if (suiteID != null)
            {
                return RedirectToAction(returnToAction, "Suite", new { id = suiteID });
            }
            else
            {
                if (scenarioID != null)
                {
                    return RedirectToAction(returnToAction, "TestScenario", new { id = scenarioID });
                }
                else
                {
                    return RedirectToAction("Index", "Suite");
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}